<?php

class indexController extends Controller
{
    private $_login;
    
    public function __construct(){
        parent::__construct();
        
    }
    
    public function index()
    {
        $this->_login = $this->loadModel('Usuarios');
        $this->_login->open();
        
        if(Session::get('access')){
            $this->redireccionar('auditorias/index');
        }
        
        $this->_view->assign('titulo', 'Iniciar Sesion');
        
        if($this->getInt('enviar') == 1){
            $this->_view->assign('datos', $_POST);
            
            $row = $this->_login->getUsuario(
                    $this->getAlphaNum('usuario'),
                    $this->getSql('pass')
                    );
            
            if(!$row){
                $this->_view->assign('_error', 'Usuario y/o password incorrectos');
                $this->_view->renderizar('index','login');
                exit;
            }
            
            if($row['activo'] != 1){
                $this->_view->assign('_error', 'Este usuario no esta habilitado');
                $this->_view->renderizar('index','login');
                exit;
            }
                        
            Session::set('access', true);
            Session::set('level', $row['descrip']);
            Session::set('datos', $row);
            Session::set('user', $row['id_usuario']);
            Session::set('time', time());
            
            $this->redireccionar('auditorias/index');
        }
        
        $this->_view->renderizar('index','login');
        
    }
    
    public function cerrar()
    {
        Session::destroy();
        $this->redireccionar('auditorias/index');
    }
}

?>
